--[[############ *Copyleft @2009-2010 Ciaho #################]]
local minFPS
local maxPing
local FPSMax = 1
local FPSLimit = 48
local FPSy
local FPSCalc
local FPSTime
local FPSgui

function CreateGuiFps( name )
		if name ~= getThisResource() then return end
		FPSLimit = 255 / FPSLimit
		--FPSgui = guiCreateLabel ( 0.475, 0.002, 1, 1, "FPS: 48", true ) --stare GUI
		FPSCalc = 0
		FPSTime = getTickCount() + 1000
		addEventHandler ( "onClientRender", getRootElement (), onClientRender )
		setTimer ( refresz, 2000, 0 ) --czas refresza
		setTimer ( function () setTimer ( sprawdzFPS, 1000, 0 ) end ,2000,1) --czas fpskickera
		setTimer ( function () setTimer ( sprawdzPing, 1000, 0 ) end ,2000,1) --czas pingkickera
		triggerServerEvent("getPingkickerSettings",getLocalPlayer())
end	


function onClientRender ( )
local ping = getPlayerPing ( getLocalPlayer ( ) )
	if ( getTickCount() < FPSTime ) then
		FPSCalc = FPSCalc + 1
	else
		if ( FPSCalc > FPSMax ) then FPSLimit = 255 / FPSCalc FPSMax = FPSCalc end
		--guiSetText ( FPSgui, " FPS: "..FPSCalc) --Stare GUI
		FPSy = FPSCalc
		FPSCalc = 0
		FPSTime = getTickCount() + 1000
	end
end	

function refresz () -- Refresz co kilka sek
setElementData ( getLocalPlayer(), "FPS" , FPSy )
end
addEventHandler ( "onClientResourceStart", getRootElement(), CreateGuiFps )

--
local rootElement = getRootElement()
local screenWidth, screenHeight = guiGetScreenSize() -- Get the screen resolution

function createText ( )
	dxDrawText( "FPS: "..FPSy, screenWidth*0.875, screenHeight*0.0119-1, screenWidth, screenHeight, tocolor ( 0, 0, 0, 255 ), 0.85, "pricedown" )  
    dxDrawText( "FPS: "..FPSy, screenWidth*0.875, screenHeight*0.0119, screenWidth, screenHeight, tocolor ( 255, 255, 255, 255 ), 0.8, "pricedown" )
end

function HandleTheRendering()
	FPSy = 48
   addEventHandler("onClientRender",rootElement, createText)
end

addEventHandler("onClientResourceStart",rootElement, HandleTheRendering)
--
local liczenie = 0
local allfps = 0
function sprawdzFPS ()
	if liczenie == 30 then
		local fpss = allfps/30
		if fpss < minFPS and minFPS ~= 0 then
			triggerServerEvent ( "onkikuj", getLocalPlayer(),"Too low FPS! ("..fpss..")" )
		else
			liczenie = 0
			allfps = 0
		end
	else
	allfps = allfps+FPSy
	liczenie = liczenie+1
	end
end

local liczeniePing = 0
local allping = 0
function sprawdzPing ()
	local ping = getPlayerPing(getLocalPlayer())
	if liczeniePing == 30 then
		local pings = allping/30
		if pings > maxPing and maxPing ~= 0 then
			triggerServerEvent ( "onkikuj", getLocalPlayer(),"Your ping is too high! ("..pings.."ms)" )
		else
	liczeniePing = 0
	allping = 0
	end
	else
	allping = allping+ping
	liczeniePing = liczeniePing+1
	end
end


function changeConfig(setting,arg)
	if setting == "fps" then
		minFPS = tonumber(arg)
	else
		maxPing = tonumber(arg)
	end
end
addEvent("changeConf",true)
addEventHandler("changeConf",getRootElement(),changeConfig)